#include <vector>
#include <iostream>
#include "../common/Entry.h"

using namespace std;

/* 基于数组简易实现的哈希表 */
class ArrayHashMap {
private:
    vector<Entry*> buckets;
public:
    ArrayHashMap() {
        buckets = vector<Entry*>(100);
    }

    /* 哈希函数 */
    int hashFunc(int key) {
        int index = key % 100;
        return index;
    }

    /* 查询操作 */
    string get(int key) {
        int index = hashFunc(key);
        Entry* pair = buckets[index];
        if (pair == nullptr)
            return nullptr;
        return pair->val;
    }

    /* 添加操作 */
    void put(int key, string val) {
        Entry* pair = new Entry(key, val);
        int index = hashFunc(key);
        buckets[index] = pair;
    }

    /* 删除操作 */
    void remove(int key) {
        int index = hashFunc(key);
        buckets[index] = nullptr;
    }

    /* 获取所有键值对 */
    vector<Entry*> entrySet() {
        vector<Entry*> entrySet;
        for (Entry* pair: buckets) {
            if (pair != nullptr) {
                entrySet.push_back(pair);
            }
        }
        return entrySet;
    }

    /* 获取所有键 */
    vector<int> keySet() {
        vector<int> keySet;
        for (Entry* pair: buckets) {
            if (pair != nullptr) {
                keySet.push_back(pair->key);
            }
        }
        return keySet;
    }

    /* 获取所有值 */
    vector<string> valueSet() {
        vector<string> valueSet;
        for (Entry* pair: buckets) {
            if (pair != nullptr){
                valueSet.push_back(pair->val);
            }
        }
        return valueSet;
    }

    /* 打印哈希表 */
    void print() {
        for (Entry* kv: entrySet()) {
            cout << kv->key << " -> " << kv->val << endl;
        }
    }
};
